home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug231 / byte.c < prev    next >
Text File  |  1987-06-17  |  3KB  |  120 lines

  1. /*
  2.     Little Smalltalk
  3.  
  4.         bytearray manipulation.
  5.         bytearrays are used almost entirely for storing bytecodes.
  6.  
  7.     timothy a. budd, 11/84
  8. */
  9. /*
  10.     The source code for the Little Smalltalk System may be freely
  11.     copied provided that the source of all files is acknowledged
  12.     and that this condition is copied with each file.
  13.  
  14.     The Little Smalltalk System is distributed without responsibility
  15.     for the performance of the program and without any guarantee of
  16.     maintenance.
  17.  
  18.     All questions concerning Little Smalltalk should be addressed to:
  19.  
  20.         Professor Tim Budd
  21.         Department of Computer Science
  22.         Oregon State University
  23.         Corvallis, Oregon
  24.         97331
  25.         USA
  26. */
  27.  
  28. # include <stdio.h>
  29. # include "object.h"
  30. # include "byte.h"
  31.  
  32. /*
  33.     bytearrays of less than MAXBSAVE are kept on a free list
  34. */
  35.  
  36. static mstruct *fr_bytearray[MAXBSAVE];     /* better be initialized to zero ! */
  37.  
  38. /*
  39.     in order to avoid a large number of small mallocs, a table is used
  40.     for the first new bytearrays.  After the table becomes full,
  41.     malloc is used to get more space.
  42.     table should be large enough for the standard prelude, at least
  43. */
  44.  
  45. static uchar btable[MAXBTABSIZE];
  46. int btabletop = 0;
  47.  
  48. /*
  49.     for the same reason, a number of bytearray bases are statically
  50.     allocated and kept on a free list
  51. */
  52.  
  53. int ca_barray = 0;
  54. static mstruct *fr_bybase = 0;
  55.  
  56. static bytearray by_init[MAXBYINIT];
  57.  
  58. byte_init()
  59. {    int i;
  60.     bytearray *p;
  61.     mstruct *new;
  62.  
  63.     p = by_init;
  64.     for (i = 0; i < MAXBYINIT; i++) {
  65.         new = (mstruct *) p;
  66.         new->mlink = fr_bybase;
  67.         fr_bybase = new;
  68.         p++;
  69.         }
  70. }
  71.  
  72. object *new_bytearray(values, size)
  73. uchar *values;
  74. int size;
  75. {    bytearray *new;
  76.     uchar *p, *q;
  77.  
  78.     if (size < MAXBSAVE && fr_bytearray[size]) {
  79.         new = (bytearray *) fr_bytearray[size];
  80.         fr_bytearray[size] = fr_bytearray[size]->mlink;
  81.         }
  82.     else {
  83.         if (fr_bybase) {
  84.             new = (bytearray *) fr_bybase;
  85.             fr_bybase = fr_bybase->mlink;
  86.             }
  87.         else {
  88.             new = structalloc(bytearray);
  89.             ca_barray++;
  90.             }
  91.         if ((btabletop + size) < MAXBTABSIZE) {
  92.             new->a_bytes = &btable[btabletop];
  93.             btabletop += size;
  94.             }
  95.         else {
  96.             new->a_bytes = (uchar *) o_alloc((unsigned) size);
  97.             }
  98.         }
  99.     new->a_ref_count = 0;
  100.     new->a_size = BYTEARRAYSIZE;
  101.     new->a_bsize = size;
  102.     for (p = new->a_bytes, q = values; size; size--) {
  103.         *p++ = *q++;
  104.         }
  105.     return((object *) new);
  106. }
  107.  
  108. free_bytearray(obj)
  109. bytearray *obj;
  110. {    int size;
  111.  
  112.     if (! is_bytearray(obj))
  113.         cant_happen(8);
  114.     size = obj->a_bsize;
  115.     if (size < MAXBSAVE) {
  116.         ((mstruct *) obj)->mlink = fr_bytearray[size];
  117.         fr_bytearray[size] = ((mstruct *) obj);
  118.         }
  119. }
  120.